package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.response.RoleResult;
import com.ihrm.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther chongxiongwei
 * @create 2020-04-19-21:23
 */
@Api(tags = {"角色控制"})
@CrossOrigin
@RestController
@RequestMapping("/sys")
@Slf4j
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 分配权限
     */
    @PutMapping("/role/assignPrem")
    public Result assignRoles(@RequestBody Map<String,Object> map){
        //1.获取被分配的角色ID
        String roleId = (String) map.get("id");
        //2.获取到权限的id列表
        List<String> permIds = (List<String>) map.get("permIds");
        //3.调用service完成权限分配
        roleService.assignPerms(roleId,permIds);
        return new Result(ResultCode.SUCCESS);
    }


    // 保存角色
    @PostMapping("/role")
    public Result save(@RequestBody Role role){
        role.setCompanyId(companyId);
        roleService.save(role);
        return new Result(ResultCode.SUCCESS);
    }

    // 修改角色
    @PutMapping("/role/{id}")
    public Result update(@PathVariable("id") String id, @RequestBody Role role){
        role.setId(id);
        roleService.update(role);
        return new Result(ResultCode.SUCCESS);
    }

    // 删除角色
    @DeleteMapping("/role/{id}")
    public Result delete(@PathVariable("id") String id){
        roleService.delete(id);
        return new Result(ResultCode.SUCCESS);
    }
    // 根据ID查询角色
    @GetMapping("/role/{id}")
    public Result findById(@PathVariable("id") String id){
        Role role = roleService.findById(id);
        RoleResult roleResult = new RoleResult(role);
        return new Result(ResultCode.SUCCESS,roleResult);
    }

    // 查询全部角色
    @GetMapping("/role")
    public Result findByPage(int page, int size, Role role){
//        Role role = roleService.findById(id);
        Page<Role> searchPage = roleService.findByPage(companyId,page,size);
        log.info("*********search Content*******"+searchPage.getContent());
        PageResult<Role> pr  = new PageResult(searchPage.getTotalElements(),searchPage.getContent());
        return new Result(ResultCode.SUCCESS,pr);
    }

    @RequestMapping(value="/role/list" ,method=RequestMethod.GET)
    public Result findAll() throws Exception {
        List<Role> roleList = roleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS,roleList);
    }
}
